/*
 * Copyright 2011 Angel Sanadinov
 *
 * This file is part of VBox WMI.
 *
 * VBox WMI is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VBox WMI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
 */

package Utilities;

import java.io.Serializable;
import Utilities.Constants.ApplicationExceptionType;

/**
 * This class represents an exception that occurred while the application was running. <br><br>
 *
 * Such an exception can come from either the application itself (e.g.: in the event of
 * invalid user input) or from VirtualBox (e.g.: an operation is not implemented).
 *
 * @see ExceptionMessages
 *
 * @author Angel Sanadinov
 */
public class ApplicationException implements Serializable
{
    private String exceptionCode;   //exception code associated with the message
    private String exceptionMessage;//exception message
    private ApplicationExceptionType exceptionType;//type of the exception

    /**
     * Constructs the exception object using the supplied data. <br><br>
     *
     * Usually used when handling exceptions coming from VirtualBox.
     *
     * @param exceptionCode code associated with the exception
     * @param exceptionMessage message associated with the exception
     */
    public ApplicationException(String exceptionCode, String exceptionMessage)
    {
        this.exceptionCode = exceptionCode;
        this.exceptionMessage = exceptionMessage;
        this.exceptionType = ApplicationExceptionType.VIRTUALBOX_EXCEPTION;
    }

    /**
     * Constructs the exception object using the supplied data. <br><br>
     *
     * Usually used when handling invalid conditions occurring within the application.
     *
     * @param exceptionCode code associated with the exception
     *
     * @see ExceptionMessages Exception Codes
     * @see ExceptionMessages#exceptionMessages Exception Messages
     */
    public ApplicationException(int exceptionCode)
    {
        this.exceptionCode = Integer.toString(exceptionCode);
        this.exceptionMessage = ExceptionMessages.exceptionMessages[exceptionCode];
        this.exceptionType = ApplicationExceptionType.APPLICATION_EXCEPTION;
    }

    /**
     * Constructs the exception object using the supplied data. <br><br>
     *
     * Usually used when handling invalid conditions occurring within the application.
     *
     * @param exceptionCode code associated with the exception
     * @param additionalData additional data to be appended to the exception message
     *
     * @see ExceptionMessages Exception Codes
     * @see ExceptionMessages#exceptionMessages Exception Messages
     */
    public ApplicationException(int exceptionCode, String additionalData)
    {
        this(exceptionCode);
        this.exceptionMessage += additionalData;
    }

    /**
     * Retrieves the code associated with this exception.
     *
     * @return the exception code
     */
    public String getExceptionCode()
    {
        return exceptionCode;
    }

    /**
     * Retrieves the message associated with this exception.
     *
     * @return the exception message
     */
    public String getExceptionMessage()
    {
        return exceptionMessage;
    }

    /**
     * Retrieves the type of this exception.
     *
     * @return the exception type
     */
    public ApplicationExceptionType getExceptionType()
    {
        return exceptionType;
    }

    /**
     * Constructs a String version of this exception. <br><br>
     *
     * The format is: exceptionCode,exceptionMessage,exceptionType
     *
     * @return this object represented as a String
     */
    @Override
    public String toString()
    {
        return exceptionCode + "," + exceptionMessage + "," + exceptionType;
    }
}
